﻿namespace UnityEditor
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using UnityEngine;

    internal sealed class ShaderUtil
    {
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern bool CheckNoTexturePropertyAndRebuild(Material mat, Shader shader, int index);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern bool DoesIgnoreProjector(Shader s);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern string GetDependency(Shader s, string name);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern int GetErrorCount(Shader s);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern string GetFallback(Shader s);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern ShaderModel GetFragmentModel(Shader s);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern int GetLOD(Shader s);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern int GetPropertyCount(Shader s);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern string GetPropertyDescription(Shader s, int propertyIdx);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern string GetPropertyName(Shader s, int propertyIdx);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern ShaderPropertyType GetPropertyType(Shader s, int propertyIdx);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern float GetRangeLimits(Shader s, int propertyIdx, int defminmax);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern int GetRenderQueue(Shader s);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern int GetShaderErrorLine(Shader s, int index);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern string GetShaderErrorMessage(Shader s, int index);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern bool GetShaderErrorWarning(Shader s, int index);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern string GetSourceChannels(Shader s);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern int GetSubShaderCount(Shader s);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern ShaderPropertyTexDim GetTexDim(Shader s, int propertyIdx);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern int GetTextureDimension(Texture t);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern ShaderModel GetVertexModel(Shader s);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern bool HasClip(Shader s);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern bool HasShadowCasterPass(Shader s);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern bool HasShadowCollectorPass(Shader s);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private static extern void INTERNAL_get_rawScissorRect(out Rect value);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private static extern void INTERNAL_get_rawViewportRect(out Rect value);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private static extern void INTERNAL_set_rawScissorRect(ref Rect value);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        private static extern void INTERNAL_set_rawViewportRect(ref Rect value);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern bool IsShaderPropertyHidden(Shader s, int propertyIdx);
        [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall]
        public static extern void OpenCompiledShader(Shader shader);

        public static bool hardwareSupportsRectRenderTexture { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; }

        public static Rect rawScissorRect
        {
            get
            {
                Rect rect;
                INTERNAL_get_rawScissorRect(out rect);
                return rect;
            }
            set
            {
                INTERNAL_set_rawScissorRect(ref value);
            }
        }

        public static Rect rawViewportRect
        {
            get
            {
                Rect rect;
                INTERNAL_get_rawViewportRect(out rect);
                return rect;
            }
            set
            {
                INTERNAL_set_rawViewportRect(ref value);
            }
        }

        public static bool wireframeMode { [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] get; [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] set; }

        internal enum ShaderModel
        {
            None,
            SM1,
            SM2,
            SM3
        }

        internal enum ShaderPropertyTexDim
        {
            TexDim2D = 2,
            TexDim3D = 3,
            TexDimAny = 6,
            TexDimCUBE = 4,
            TexDimDeprecated1D = 1,
            TexDimNone = 0,
            TexDimRECT = 5,
            TexDimUnknown = -1
        }

        internal enum ShaderPropertyType
        {
            Color,
            Vector,
            Float,
            Range,
            TexEnv
        }
    }
}

